package com.sergio.compress.cp.mainUtil;

import com.sergio.compress.cp.base.CompressResult;
import com.sergio.compress.cp.base.CompressTest;
import com.sergio.compress.cp.base.DecompressResult;
import com.sergio.compress.cp.base.TestSource;
import org.springframework.util.StopWatch;
import org.xerial.snappy.Snappy;

import java.io.IOException;
import java.util.Date;

/**
 * @Author : Sergio.D
 * @Description :
 * @CreateDate ：2023/6/14 17:40
 */
public class SNAPPY extends CompressTest {

    public CompressResult compress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        StopWatch stopWatch = new StopWatch();
            stopWatch.start();
        byte[] compressed = Snappy.compress(data);
        stopWatch.stop();
        return new CompressResult(getType(), stopWatch.getTotalTimeNanos(), compressed, data.length, compressed.length);
    }

    public DecompressResult decompress(byte[] data) throws IOException {
        if (data == null || data.length == 0) {
            return null;
        }
        StopWatch stopWatch = new StopWatch();
            stopWatch.start();
        byte[] decompressed = Snappy.uncompress(data);
        stopWatch.stop();
        return new DecompressResult(getType(), stopWatch.getTotalTimeNanos(), decompressed, decompressed.length, decompressed.length);
    }

    @Override
    public String getType() {
        return "SNAPPY";
    }

    @Override
    public CompressResult compress(TestSource testSource) throws IOException {
        return compress(testSource.getSourceData());
    }

    @Override
    public DecompressResult decompress(CompressResult testSource) throws IOException {
        return decompress(testSource.getData());
    }
}
